﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;

public class ButtonUIElement : BaseUIElement
{
    protected override void SetMethod(BasePanel panel)
    {
        MethodInfo mi = panel.GetType().GetMethod("OnClick" + uiElementName);
        BindMethod(mi, panel);
    }

    public override void BindMethod(MethodInfo Method, BasePanel panel)
    {
        gameObject.GetComponent<Button>().onClick.RemoveAllListeners();
        gameObject.GetComponent<Button>().onClick.AddListener(delegate() {
            try
            {
                Method.Invoke(panel, parameters);

            }
            catch(Exception e) {
                if (e.ToString().Split(':')[0] == "System.Reflection.TargetParameterCountException")
                {
                    Debug.LogWarning("按钮点击调用方法参数不匹配:" + "OnClick" + uiElementName + "()");
                }
                else {
                    Debug.LogWarning("没有找到按钮点击对应的方法:" + "OnClick" + uiElementName + "()");
                }
            }
        });
    }

}
